/*
 * Created by JFormDesigner on Thu Jun 15 11:02:35 CST 2023
 */

package fm.type;

import dao.Dao_type;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class TypeAdd extends JInternalFrame {
    public TypeAdd() {
        initComponents();
    }

    private void Cancel(ActionEvent e) {
        // TODO add your code here
        doDefaultCloseAction();
    }

    private void add(ActionEvent e) {
        List<String> list = Dao_type.getTypeID();
        if(typeIDTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入类别编号");
        }

        for(String s: list)
        {
            if(s.equals(typeIDTextField.getText()))
            {
                JOptionPane.showMessageDialog(null,"商品类型ID已存在，请重新设置商品类型编号！");
                return;
            }
        }

        if(typeIDTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入类别编号");
        }

        if(typeTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入类别名称！");
            return;
        }

        if(minTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入商品下限！");
            return;
        }

        int rap = Integer.parseInt(minTextField.getText().trim());
        if(rap <= 0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的下限！");
            return;
        }

        if(maxTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入上限！");
            return;
        }

        int ycy = Integer.parseInt(maxTextField.getText().trim());
        if(ycy <= 0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的上限！");
            return;
        }

        if(ycy <= rap)
        {
            JOptionPane.showMessageDialog(null,"上限小于下限！！");
            return;
        }

        String typeID = typeIDTextField.getText().trim();
        String type = typeTextField.getText().trim();
        String min = minTextField.getText().trim();
        String max = maxTextField.getText().trim();

        int i = Dao_type.add(typeID, type, Integer.parseInt(min), Integer.parseInt(max));

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"添加成功！");
            doDefaultCloseAction();
        }
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        addButton = new JButton();
        CancelButton = new JButton();
        panel3 = new JPanel();
        label1 = new JLabel();
        typeIDTextField = new JTextField();
        label2 = new JLabel();
        minTextField = new JTextField();
        maxTextField = new JTextField();
        label3 = new JLabel();
        typeTextField = new JTextField();
        label4 = new JLabel();
        label5 = new JLabel();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("\u589e\u52a0\u5546\u54c1\u7c7b\u522b");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(332, 40));
            panel1.setLayout(null);

            //---- addButton ----
            addButton.setText("\u6dfb\u52a0");
            addButton.addActionListener(e -> add(e));
            panel1.add(addButton);
            addButton.setBounds(new Rectangle(new Point(205, 5), addButton.getPreferredSize()));

            //---- CancelButton ----
            CancelButton.setText("\u53d6\u6d88");
            CancelButton.addActionListener(e -> Cancel(e));
            panel1.add(CancelButton);
            CancelButton.setBounds(new Rectangle(new Point(280, 6), CancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 304, 528, panel1.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setBorder(new TitledBorder("\u586b\u5199\u5546\u54c1\u7c7b\u578b\u4fe1\u606f"));
            panel3.setLayout(null);

            //---- label1 ----
            label1.setText("\u7c7b\u578b\u7f16\u53f7\uff1a");
            panel3.add(label1);
            label1.setBounds(56, 45, label1.getPreferredSize().width, 21);
            panel3.add(typeIDTextField);
            typeIDTextField.setBounds(116, 41, 120, typeIDTextField.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u4e0b\u9650\uff1a");
            panel3.add(label2);
            label2.setBounds(79, 110, 40, 21);
            panel3.add(minTextField);
            minTextField.setBounds(117, 105, 119, 28);
            panel3.add(maxTextField);
            maxTextField.setBounds(340, 106, 119, 28);

            //---- label3 ----
            label3.setText("\u4e0a\u9650\uff1a");
            panel3.add(label3);
            label3.setBounds(300, 111, 40, 21);
            panel3.add(typeTextField);
            typeTextField.setBounds(339, 38, 120, 28);

            //---- label4 ----
            label4.setText("\u7c7b\u578b\u540d\u79f0\uff1a");
            panel3.add(label4);
            label4.setBounds(279, 44, 60, 21);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 150, 528, 154);

        //---- label5 ----
        label5.setIcon(new ImageIcon(getClass().getResource("/ref/trade.png")));
        contentPane.add(label5);
        label5.setBounds(-5, 0, 545, 170);

        contentPane.setPreferredSize(new Dimension(540, 375));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton addButton;
    private JButton CancelButton;
    private JPanel panel3;
    private JLabel label1;
    private JTextField typeIDTextField;
    private JLabel label2;
    private JTextField minTextField;
    private JTextField maxTextField;
    private JLabel label3;
    private JTextField typeTextField;
    private JLabel label4;
    private JLabel label5;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
